package com.sgy.sgylearningproject.mybatis.order_user.mapper;

import com.sgy.sgylearningproject.mybatis.order_user.pojo.Role;
import com.sgy.sgylearningproject.mybatis.order_user.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.cache.impl.PerpetualCache;
import org.mybatis.caches.redis.RedisCache;

import java.util.List;

//@CacheNamespace(implementation = PerpetualCache.class) // 开启二级缓存，基于PerpetualCache实现二级缓存
@CacheNamespace(implementation = RedisCache.class) // 开启二级缓存，基于RedisCache实现二级缓存
public interface IUserMapper {

    // 查询所有用户信息和用户关联的订单信息
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "orderList", column = "id", javaType = List.class,
                    many = @Many(select = "com.sgy.sgylearningproject.mybatis.order_user.mapper.IOrderMapper.findOrderByUid"))
    })
    @Select("select * from user")
    public List<User> findAll();

    // 查询所有用户信息和用户关联的角色信息
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "roleList", column = "id", javaType = List.class,
                    many = @Many(select = "com.sgy.sgylearningproject.mybatis.order_user.mapper.IRoleMapper.findRoleByUid"))
    })
    @Select("select * from user")
    public List<User> findAllUserAndRole();

    // 添加用户
    @Insert("insert into user(id,username) values(#{id},#{username})")
    public void addUser(User user);

    // 更新用户
    @Update("update user set username=#{username} where id=#{id}")
    public void updateUser(User user);

    // 查询用户
    @Select("select * from user")
    public List<User> selectUser();

    // 删除用户
    @Delete("delete from user where id=#{id}")
    public void deleteUser(Integer id);

    // 根据id查询用户
//    @Options(useCache = false) // 禁用二级缓存
    @Select("select * from user where id = #{id}")
    public User findUserById(Integer id);
}
